package com.czp.infosharingplatformapp.controller;

import com.czp.infosharingplatformapp.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private WebClient.Builder webClientBuilder;

    @Value("${server.port}")
    private String serverPort;

    private String getActuatorUrl(String endpoint) {
        return "http://localhost:" + serverPort + "/actuator/" + endpoint;
    }

    @GetMapping("/metrics")
    public Mono<String> getMetrics() {
        WebClient webClient = webClientBuilder.build();
        return webClient.get()
                .uri(getActuatorUrl("metrics"))
                .retrieve()
                .bodyToMono(String.class);
    }

    @GetMapping("/health")
    public Mono<String> getHealth() {
        WebClient webClient = webClientBuilder.build();
        return webClient.get()
                .uri(getActuatorUrl("health"))
                .retrieve()
                .bodyToMono(String.class);
    }

    @GetMapping("/env")
    public Mono<String> getEnv() {
        WebClient webClient = webClientBuilder.build();
        return webClient.get()
                .uri(getActuatorUrl("env"))
                .retrieve()
                .bodyToMono(String.class);
    }

    @GetMapping("/info")
    public Mono<String> getInfo() {
        WebClient webClient = webClientBuilder.build();
        return webClient.get()
                .uri(getActuatorUrl("info"))
                .retrieve()
                .bodyToMono(String.class);
    }

    @GetMapping("/beans")
    public Mono<String> getBeans() {
        WebClient webClient = webClientBuilder.build();
        return webClient.get()
                .uri(getActuatorUrl("beans"))
                .retrieve()
                .bodyToMono(String.class);
    }
}